<?php

class Bemigo_MypageController extends Bemigo_Controller_Action_Base
{

	public function init()
	{
		/* Initialize action controller here */
		$contextSwitch = $this->_helper->getHelper('AjaxContext');
		$contextSwitch
			->addActionContext('adduser', 'json')
			->addActionContext('addfriend', 'json')
			->addActionContext('cachefriends', 'json')
			->addActionContext('resetcache', 'json')
			->addActionContext('test', 'json')
			->initContext();
		parent::init();
		$this->checkFBAuth();
	}

	public function indexAction()
	{
		$user_profile = $this->getUserProfile();
		$this->addUser($user_profile['id'], $user_profile['name']);
		$this->view->user_friends = $this->getUserFriends();
		$this->view->user_id = $this->_facebook->getUser();
		$this->view->user_profile = $user_profile;
		$this->view->fb_app_id = Zend_Registry::get("configuration")->facebook->app->id;
	}

	protected function getUserProfile(){
		$user_profile = array();
		$manager = $this->getFrontController()->getParam('bootstrap')->getResource('cachemanager');
		$cache = $manager->getCache('bemigo');
		if(!$user_profile = $cache->load("profile_".$this->_facebook->getUser())){
			$user_profile = $this->_facebook->api('/me');
			$cache->save($user_profile, "profile_".$this->_facebook->getUser());
		}
		return $user_profile;
	}

	protected function getUserFriends(){
		$friends = array();
		$manager = $this->getFrontController()->getParam('bootstrap')->getResource('cachemanager');
		$cache = $manager->getCache('bemigo');
		if(!$friends = $cache->load("friends_".$this->_facebook->getUser())){
			$friends = $this->_facebook->api('/me/friends');
			$cache->save($friends, "friends_".$this->_facebook->getUser());
		}
		return $friends;
	}

	public function addfriendAction(){
		$em = $this->getEm();
		$my_id = $this->_request->getParam("myid", null);
		if($my_id != $this->_facebook->getUser()){
			throw new Exception("Permission denied. Please logout and log back in.", 0);
		}
		$dql = "SELECT u FROM Entities\Users u WHERE u.facebookId =?1";
		$query = $em->createQuery($dql)->setParameter(1, $my_id);
		$user = $query->getResult();

		if(!empty($user[0])){
			$fb_id = $this->_request->getParam("fbid", null);
			$fb_friend = $this->_facebook->api('/'.$fb_id);
			$friend = $this->addUser($fb_friend['id'], $fb_friend['name']);
			$relation = $this->addFriend($user[0], $friend);
			$this->view->result = $relation->getId();
		}else{
			throw new Exception("User mismatch. Permission denied. Please logout and log back in.", 0);
		}
	}

	protected function addUser($fb_id, $fb_name){

		$em = $this->getEm();
		$dql = "SELECT u FROM Entities\Users u WHERE u.facebookId =?1";

		$query = $em->createQuery($dql)->setParameter(1, $fb_id);
		$user = $query->getResult();
		if(empty($user)){
			$user = new \Entities\Users();
			$user->setFacebookId($fb_id);
			$user->setName($fb_name);
			$em->persist($user);
		}else{
			return $user[0];
		}
		$em->flush();
		return $user;
	}

	protected function addFriend($user, $friend){
		$em = $this->getEm();
		$dql = "SELECT f FROM Entities\Friends f WHERE f.user =?1 AND f.friend=?2";
		$query = $em->createQuery($dql)->setParameter(1, $user)->setParameter(2, $friend);
		$relation = $query->getResult();
		if(empty($relation)){
			$relation = new \Entities\Friends();
			$relation->setUser($user);
			$relation->setFriend($friend);
			$em->persist($relation);
		}else{
			return $relation[0];
		}
		$em->flush();
		return $relation;
	}

	public function testAction(){

	}

}

